package com.yx.controller;

import com.yx.common.R;
import com.yx.constatns.RabbitMQConstatns;
import com.yx.feign.api.GoodsService;
import com.yx.vo.GoodsType;
import lombok.NoArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname GoodsTypeController
 * @Date 2021/07/07 16:34
 * @Author by yx
 * @Description TODO:
 */
@Controller
@RequestMapping("/goodsTypeController")
public class GoodsTypeController {
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/{page}.html")
    public String toPage(@PathVariable String page){
        return "goodsType/"+page;
    }

    @GetMapping("/addGoodsType/{id}")
    public String toAddPage(@PathVariable Integer id,ModelMap modelMap){
        modelMap.put("id", id);
        return "goodsType/addGoodsType";
    }

    @GetMapping("/getGoodsTypeList")
    public String getGoodsTypeList(ModelMap modelMap){
        List<GoodsType> goodsTypeList = goodsService.getGoodsTypeList();
        modelMap.put("goodsTypeList", goodsTypeList);
        return "goodsType/goodsTypeList";
    }

    @PostMapping("/addGoodsType")
    @ResponseBody
    public R addGoodsType(GoodsType goodsType){
        System.out.println(goodsType.getGoodsTypeName());
        rabbitTemplate.convertAndSend(RabbitMQConstatns.GOODS_EXCHANGE, "shop.goodsType.insert", "强制更新商品类型数据");
        return R.write(goodsService.addGoodsType(goodsType));
    }

    @PostMapping("/updateGoodsType")
    @ResponseBody
    public R updateGoodsType(GoodsType goodsType){
        System.out.println(goodsType);
        return R.write(goodsService.updateGoodsType(goodsType));
    }

    @GetMapping("/deleteGoodsType/{id}")
    @ResponseBody
    public R deleteGoodsType(@PathVariable("id") Integer id){
        return R.write(goodsService.deleteGoodsType(id));
    }

    @GetMapping("/getGoodsTypeTree")
    @ResponseBody
    public R getGoodsTypeTree(){
        //1.创建list集合存储所有节点信息
        List<Map<String,Object>> nodeList = new ArrayList<>();

        //2.遍历节点,存入map集合,放入list中
        List<GoodsType> goodsTypeList = goodsService.getGoodsTypeList();
        for (GoodsType goodsType : goodsTypeList) {
            //3.创建一个map集合存储节点数据
            Map<String,Object> node = new HashMap<>();
            node.put("id", goodsType.getId());
            node.put("name", goodsType.getGoodsTypeName());
            node.put("pid", goodsType.getGoodsTypePid());

            //4.添加到list集合中
            nodeList.add(node);
        }
        return R.success(nodeList);
    }

    /**
     * @Author:yx
     * @return com.yx.common.R
     * @Date 2021/07/08 16:18
     * @param pid
     * @param id
     * @Description: goodsType树型节点拖拽功能实现
     **/
    @GetMapping("/updateGoodsTypePid/{pid}/{id}")
    @ResponseBody
    public R updateGoodsTypePid(@PathVariable("pid") Integer pid,@PathVariable("id") Integer id){
        //1.查询此id下的商品信息
        GoodsType goodsTypeById = goodsService.getGoodsTypeById(id);
        //改变该商品的pid
        goodsTypeById.setGoodsTypePid(pid);
        //更新数据库
        Boolean aBoolean = goodsService.updateGoodsType(goodsTypeById);

        return R.write(aBoolean);
    }


    @GetMapping("/getGoodsTypeListByPid/{pid}")
    @ResponseBody
    public List<GoodsType> getGoodsTypeListByPid(@PathVariable("pid") Integer pid){
        return goodsService.getGoodsTypeListByPid(pid);
    }
}
